# coding: utf-8

from django.http import HttpResponse
from django.shortcuts import render_to_response

import datetime

from utils import columnize, rowize, block_firsts, blocks, UnicodeChar

# source for charmaps: http://docs.python.org/_sources/library/codecs.txt
legacy_codes = (
    (u'Western Europe',(
         'latin1 iso-8859-1 iso8859-1 8859 cp819 latin latin-1 L1',
         'cp1140 ibm1140',
         'cp1252 windows-1252',
         'cp500 EBCDIC-CP-BE EBCDIC-CP-CH IBM500',
         'cp850 850 IBM850',
         'iso8859_15 iso-8859-15',
         'mac_roman macroman',
    ),),
    (u'Arabic',(
        'cp1256 windows1256',
        'cp864 IBM864',
        'iso8859_6 iso-8859-6 arabic',
    ),),
    (u'Baltic languages',(
        'cp1257 windows-1257',
        'cp775 IBM775',
        'iso8859_13 iso-8859-13',
        'iso8859_4 iso-8859-4 latin4 L4',
    ),),
    (u'Bulgarian, Byelorussian, Macedonian, Russian, Serbian',(
        'cp1251 windows-1251',
        'cp855 855 IBM855',
        'iso8859_5 iso-8859-5 cyrillic',
        'mac_cyrillic maccyrillic',
    ),),
    (u'Canadian',(
        'cp863 863 IBM863',
    ),),
    (u'Celtic languages',(
        'iso8859_14 iso-8859-14, latin8, L8',
    ),),
    (u'Central and Eastern Europe',(
        'cp1250 windows-1250',
        'cp852 852 IBM852',
        'iso8859_2 iso-8859-2 latin2 L2',
        'mac_latin2 maclatin2 maccentraleurope',
    ),),
    (u'Danish, Norwegian',(
        'cp865 865 IBM865',
    ),),
    (u'English',(
        'ascii iso646 646 us-ascii',
        'cp037 IBM037 IBM039',
        'cp437 437 IBM437',
    ),),
    (u'Esperanto, Maltese',(
        'iso8859_3 iso-8859-3 latin3 L3',
    ),),
    (u'Greek',(
        'cp1253 windows-1253',
        'cp737',
        'cp869 869 CP-GR IBM869',
        'cp875',
        'iso8859_7 iso-8859-7 greek greek8',
        'mac_greek macgreek',
    ),),
    (u'Hebrew',(
        'cp1255 windows-1255',
        'cp424 EBCDIC-CP-HE IBM424',
        'cp856',
        'cp862 862 IBM862',
        'iso8859_8 iso-8859-8 hebrew',
    ),),
    (u'Icelandic',(
        'cp861 861 CP-IS IBM861',
        'mac_iceland maciceland',
    ),),
    (u'Nordic languages',(
        'iso8859_10 iso-8859-10 latin6 L6',
    ),),
    (u'Portuguese',(
        'cp860 860, IBM860',
    ),),
    (u'Russian',(
        'cp866 866 IBM866',
        'koi8_r',
    ),),
    (u'Thai',(
        'cp874',
    ),),
    (u'Turkish',(
        'cp1026 ibm1026',
        'cp1254 windows-1254',
        'cp857 857 IBM857',
        'iso8859_9 iso-8859-9 latin5 L5',
        'mac_turkish macturkish',
    ),),
    (u'Ukrainian',(
        'koi8_u',
    ),),
    (u'Urdu',( 
        'cp1006',
    ),),
    (u'Vietnamese',(
        'cp1258 windows-1258',
    ),),
)    

def main(request):
    page_title = u'Unibabel V.1'
    block_pages = [dict(title=title, url='/block/%x'%first) 
                   for first, l, title in blocks]
    legacy_pages = []
    for group_name, pages in legacy_codes:
        group = {'name': group_name}
        group['pages'] = []
        for page in pages:
            encodings = page.split()
            encoding = encodings[0]
            if len(encodings) > 1:
                aliases = '(%s)' % ', '.join(encodings[1:])
            else:
                aliases = ''
            group['pages'].append(dict(url='/legacy/%s/'%encoding,
                               encoding=encoding,
                               aliases=aliases))
        legacy_pages.append(group)
    return render_to_response('mainpage.django.html', locals())

COLS = 32

def block_page(request, first):
    first = int(first, 16)
    last, page_title = block_firsts[first]
    scope = ('U+%04x...U+%04x' % (first, last)).upper()
    columns = rowize([UnicodeChar(i) for i in xrange(first, last+1)], COLS)
    col_heads = ['%x'%(i%0x10) for i in range(COLS)]
    return render_to_response('chartable.django.html', locals())

def legacy_page(request, encoding, title, aliases):
    page_title = title
    first, last = (0x00, 0xFF)
    chars = []
    for i in xrange(0xFF):
        try:
            char = ord(chr(i).decode(encoding))
        except UnicodeDecodeError:
            char = 0
        chars.append(char)    
    
    columns = rowize([UnicodeChar(i) for i in chars], COLS)
    col_heads = ['%x'%(i%0x10) for i in range(COLS)]
    return render_to_response('chartable.django.html', locals())
    
def char_detail(request, codepoint):
    char = UnicodeChar(int(codepoint, 16))
    return render_to_response('chardetail.django.html', locals())
    
